package com.xiao.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description Json相关转换工具类，fixme 这里面抛出的异常需要等待全局异常处理完成后重构
 * @Auther: 笑笑是一个码农
 * @Date: 15:39 2021/1/5
 */
@Slf4j
public class JsonUtils {

    public static final ObjectMapper mapper = new ObjectMapper();
    public static final ObjectMapper mapperFlex = createFlexObjectMapper();

    private static ObjectMapper createFlexObjectMapper() {
        ObjectMapper r = new ObjectMapper();
        r.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return r;
    }

    /**
     * 将对象序列化成json
     * @param obj
     * @return
     */
    public static String serialize(@NonNull Object obj) {
        if (obj.getClass() == String.class) {
            return (String) obj;
        }
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("json序列化出错: target: {}, exception: {}",obj,e);
            throw new RuntimeException("json序列化出错");
        }
    }

    /**
     * json反序列化成对象，严格模式，如果对象中不存的属性，会抛出异常
     * @param json
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> T parse(@NonNull String json, Class<T> tClass) {
        try {
            return mapper.readValue(json, tClass);
        } catch (IOException e) {
            log.error("json解析出错: target: {}, exception: {}",json,e);
            throw new RuntimeException("json解析出错");
        }
    }

    /**
     * json反序列化成对象，宽松模式，不存在的属性不会抛出异常
     * @param json
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> T parseFlex(@NonNull String json, Class<T> tClass) {
        try {
            return mapperFlex.readValue(json, tClass);
        } catch (IOException e) {
            log.error("json解析出错: target: {}, exception: {}",json,e);
            throw new RuntimeException("json解析出错");
        }
    }

    /**
     * json反序列化成list集合
     * @param json
     * @param eClass
     * @param <E>
     * @return
     */
    public static <E> List<E> parseList(@NonNull String json, Class<E> eClass) {
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, eClass));
        } catch (IOException e) {
            log.error("json解析出错: target: {}, exception: {}",json,e);
            throw new RuntimeException("json解析出错");
        }
    }

    /**
     * json反序列化成set集合
     * @param json
     * @param eClass
     * @param <E>
     * @return
     */
    public static <E> Set<E> parseSet(@NonNull String json, Class<E> eClass) {
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(Set.class, eClass));
        } catch (IOException e) {
            log.error("json解析出错: target: {}, exception: {}",json,e);
            throw new RuntimeException("json解析出错");
        }
    }

    /**
     * json反序列化成list集合, 宽松模式
     * @param json
     * @param eClass
     * @param <E>
     * @return
     */
    public static <E> List<E> parseListFlex(@NonNull String json, Class<E> eClass) {
        try {
            return mapperFlex.readValue(json, mapperFlex.getTypeFactory().constructCollectionType(List.class, eClass));
        } catch (IOException e) {
            log.error("json解析出错: target: {}, exception: {}",json,e);
            throw new RuntimeException("json解析出错");
        }
    }

    /**
     * json反序列化成map集合
     * @param json
     * @param kClass
     * @param vClass
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> parseMap(@NonNull String json, Class<K> kClass, Class<V> vClass) {
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));
        } catch (IOException e) {
            log.error("json解析出错: target: {}, exception: {}",json,e);
            throw new RuntimeException("json解析出错");
        }
    }

    /**
     * json反序列化成map集合,宽松模式
     * @param json
     * @param kClass
     * @param vClass
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> parseMapFlex(@NonNull String json, Class<K> kClass, Class<V> vClass) {
        try {
            return mapperFlex.readValue(json, mapperFlex.getTypeFactory().constructMapType(Map.class, kClass, vClass));
        } catch (IOException e) {
            log.error("json解析出错: target: {}, exception: {}",json,e);
            throw new RuntimeException("json解析出错");
        }
    }

    public static <T> T nativeRead(@NonNull String json, TypeReference<T> type) {
        try {
            return mapper.readValue(json, type);
        } catch (IOException e) {
            log.error("json解析出错: target: {}, exception: {}",json,e);
            throw new RuntimeException("json解析出错");
        }
    }

    public static <T> T nativeReadFlex(@NonNull String json, TypeReference<T> type) {
        try {
            return mapperFlex.readValue(json, type);
        } catch (IOException e) {
            log.error("json解析出错: target: {}, exception: {}",json,e);
            throw new RuntimeException("json解析出错");
        }
    }
}
